﻿using UnityEngine;

namespace GX.Singletion
{
    public class MonoSingletion<T> : MonoBehaviour where T : MonoBehaviour
    {
        protected static T instance;
        public static T Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = UnityEngine.Object.FindObjectOfType(typeof(T)) as T;
                    if (instance == null)
                    {
                        GameObject a = GameObject.Find("SingletionRoot");
                        if (a == null)
                            a = new GameObject("SingletionRoot");
                        GameObject instanceGO = new GameObject(typeof(T).Name);
                        instance = instanceGO.AddComponent<T>();
                        instanceGO.transform.parent = a.transform;
                    }
                }
                return instance;
            }
        }
    }
}
